package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.SpaceDrawingDTO;
import com.eastfair.home.dto.SpaceDrawingPageQuery;
import com.eastfair.home.dto.SpaceDrawingSaveDTO;
import com.eastfair.home.dto.SpaceDrawingUpdateDTO;
import com.eastfair.home.entity.SpaceDrawing;
import com.eastfair.home.service.SpaceDrawingService;
import com.eastfair.home.vo.SpaceDrawingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展厅图纸
 * </p>
 *
 * @author dq
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/spaceDrawing")
@Api(value = "SpaceDrawing", tags = "展厅图纸")
@PreAuth(replace = "home:spaceDrawing:")
public class SpaceDrawingController extends SuperController<SpaceDrawingService, Long, SpaceDrawing, SpaceDrawingPageQuery, SpaceDrawingSaveDTO, SpaceDrawingUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private SpaceDrawingService spaceDrawingService;

    @Override
    public void handlerResult(IPage<SpaceDrawing> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<SpaceDrawing> spaceDrawingList = list
                .stream()
                .map((map) -> SpaceDrawing.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(spaceDrawingList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SpaceDrawingPageQuery> params) {
        SpaceDrawingPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 查询展厅图纸VO列表.
     *
     * @param spaceDrawingDTO the space drawing dto
     * @return the r
     */
    @ApiOperation("查询展厅图纸VO列表")
    @PostMapping("/query/spaceDrawing")
    public R<List<SpaceDrawingVO>> querySpaceDrawing(@RequestBody SpaceDrawingDTO spaceDrawingDTO) {
        log.info("querySpaceDrawing - 查询展厅图纸VO列表, spaceDrawingDTO={}", spaceDrawingDTO);
        return R.successDef(spaceDrawingService.listSpaceDrawingVOs(spaceDrawingDTO));
    }
}
